package com.mysoftbase.base.intercetpor;

import com.mysoft.common.DataSourceEnum;
import com.mysoftbase.base.holder.DataSourceContextHolder;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Author: JohnCoding
 * @Description:
 * @date: 2019/12/26 9:32
 */
@Component
public class BaseIntercetpor implements HandlerInterceptor {


    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //默认设置主数据源
        DataSourceContextHolder.setDataSource(DataSourceEnum.MASTER.getValue());
        System.out.println("执行方法前");
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        System.out.println("执行方法后,DispatcherServlet渲染视图前");

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        System.out.println("DispatcherServlet完全处理完请求后(DispatcherServlet渲染视图后)");
    }
}
